﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Basics
{
    static public class FpsMeter
    {
        
        static public uint Fps
        {
            get { return fps; }
        }

        static private SpriteFont font;

        static public SpriteFont Font
        {
          get { return font; }
          set { font = value; }
        }

        static private Vector2 position;

        static public Vector2 Position
        {
          get { return position; }
          set { position = value; }
        }

        static private uint fps;
        static private uint counter;
        static private TimeSpan span = new TimeSpan();
        static private TimeSpan last;
        static private SpriteBatch spriteBatch;

        static public void Initialize(Vector2 position, SpriteFont font)
        {
            FpsMeter.position = position;
            FpsMeter.font = font;

            fps = 0;
            counter = 0;
            span = new TimeSpan(0, 0, 0);
            last = new TimeSpan(0, 0, 0);
            spriteBatch = new SpriteBatch(Program.game.GraphicsDevice);
        }

        static public void Draw(GameTime gameTime)
        {
            FpsMeter.Update(gameTime);
            spriteBatch.Begin();
            spriteBatch.DrawString(FpsMeter.font, fps.ToString(), FpsMeter.position, Color.White);
            spriteBatch.End();
        }

        static private void Update(GameTime gameTime)
        {
            if (span.TotalMilliseconds < 1000)
            {
                counter++;
                span = (gameTime.TotalGameTime - last);
            }
            else
            {
                fps = counter;
                counter = 0;
                span = new TimeSpan(0, 0, 0);
                last = gameTime.TotalGameTime;
            }
        }
    }
}
